<template lang="pug">
.list
  .header
    .header-search
      el-input(placeholder="电子拼板名称/发布者" prefix-icon="el-icon-search" clearable v-model="searchData" @change="changeSearch")
      el-date-picker(
        placeholder="选择时间"
        type="daterange"
        v-model="time"
        format="yyyy-MM-dd"
        value-format="yyyy-MM-dd"
        range-separator="-"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        @change="changeTime"
        clearable
      )
    .header-btn
      el-button(size="small" name="info" @click="batchDelete" v-if="$permission('AB-05002-001')") 批量删除
  .list-table
    el-table(
      height="100%"
      :data="list"
      :header-cell-style="{ fontSize: '14px', color: '#64698F', background: '#F8F7FC' }"
      border
      style="width: 100%"
      v-loading="loading"
      @selection-change="handleSelectionChange"
      empty-text="未找到符合条件的内容"
    )
      el-table-column(type="selection" width="55" align="center")
      el-table-column(type="index" width="55" align="center" label="序号")
      el-table-column(label="电子拼板" prop="")
        template(slot-scope="scope")
          .table-item-img(:style="scope.row.preview | filterUrl")
      el-table-column(label="电子拼板名称" prop="title")
        template(slot-scope="scope")
          .table-item-title {{scope.row.title}}
      el-table-column(label="发布者" prop="userName")
      el-table-column(label="发起时间" prop="createTime" sortable)
      el-table-column(label="操作")
        template(slot-scope="scope")
          .table-item-handle
            .table-item-btn.info(@click="delItem(scope.row)" v-if="$permission('AB-05002-001')") 删除
  .table-pagination
    el-pagination(
      background
      layout="prev, pager, next, jumper"
      :total="total"
      :current-page="page"
      :page-size="pageSize"
      @current-change="currentChange"
    )
</template>

<script>
import { getPuzzleList, postDeletePuzzle } from 'api'
export default {
  name: 'puzzleList',
  data () {
    return {
      page: 1,
      pageSize: 20,
      loading: false,
      searchData: '',
      time: [],
      list: [],
      total: 0,
      selectionList: []
    }
  },
  methods: {
    changeSearch () {
      this.page = 1
      this.getList()
    },
    changeTime () {
      this.page = 1
      this.getList()
    },
    currentChange (page) {
      this.page = page
      this.getList()
    },
    batchDelete () {
      if (!this.selectionList.length) {
        this.$message.error('请先选择要删除的数据！')
        return
      }
      this.$confirm('是否确定删除选择电子拼板？', '批量删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(async () => {
        try {
          let idList = this.selectionList.map(item => item.id).join(',')
          await this.$axios.post(postDeletePuzzle, { ids: idList })
          this.$message.success('删除成功！')
          this.getList()
        } catch (err) {
          console.log(err)
          this.$message.error(err.message || err.ResultSet.errors)
        }
      })
    },
    delItem (row) {
      this.$confirm('是否确定删除该电子拼板？', '删除拼版', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(async () => {
        try {
          await this.$axios.post(postDeletePuzzle, { ids: row.id + '' })
          this.$message.success('删除成功！')
          this.getList()
        } catch (err) {
          console.log(err)
          this.$message.error(err.message || err.ResultSet.errors)
        }
      })
    },
    handleSelectionChange (rows) {
      this.selectionList = rows
    },
    async getList () {
      try {
        this.loading = true
        let query = { ...this.$route.query }
        query.currentPage = this.page
        query.pageLines = this.pageSize
        if (this.searchData) query.searchData = this.searchData
        if (this.time && this.time.length) {
          query.startDate = this.time[0]
          query.endDate = this.time[1]
        }
        const { ResultSet } = await this.$axios.post(getPuzzleList, { ...query })
        this.total = ResultSet.recordNumber
        this.list = ResultSet.userData.list
        this.loading = false
      } catch (err) {
        this.loading = false
        console.log(err)
        this.$message.error(err.message || err.ResultSet.errors)
      }
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.getList()
    })
  }
} 
</script>

<style lang="scss" scoped>
.list {
  width: 100%;
  height: 100%;
  background: #FFF;
  border: 1px solid #F3F3F4;
  box-sizing: border-box;
  box-shadow: 0px 4px 0px #F3F3F4;
  border-radius: 10px;
  padding: 0 40px;
}
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 0;
  .el-input {
    width: 202px;
    margin-right: 16px;
  }
  .el-select {
    margin-right: 16px;
  }
  .header-search {
    display: flex;
    align-items: center;
  }
  .header-info {
    font-size: 14px;
    line-height: 22px;
    color: #8B8EAB;
  }
}
.list-table {
  height: calc(100% - 150px);
}
.table-pagination {
  padding: 24px 0;
  text-align: right;
}
.table-item-img {
  width: 140px;
  height: 100px;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}
.table-item-title {
  cursor: pointer;
  color: #FF2E42;
}
.table-item-handle {
  display: flex;
  .table-item-btn {
    margin-right: 8px;
    padding: 5px 8px;
    font-size: 12px;
    line-height: 20px;
    border-radius: 6px;
    cursor: pointer;
    &.warning {
      background: #FDEFF1;
      color: #FF2E42;
    }
    &.normal {
      background: #F3F0FF;
      color: #7362F0;
    }
    &.info {
      background: #F6F6F9;
      color: #3E4373;
    }
  }
}
</style>
